#ifndef EXAMPLEDATA_H
#define EXAMPLEDATA_H
#include <QString>
#include <QByteArray>
#include <QDataStream>

//对象数据序列化模板类
class ExampleData
{
public:
    QString str1 = "";
    QString str2 = "";
    int i1 = 0;
    int i2 = 0;
    bool b1 = 0;
    bool b2 = 0;
    float f1 = 0.0;
    float f2 = 0.0;
    QByteArray hex1;
    QByteArray hex2;

    //下面重载操作符不可使得类不可继承QObject类（继承QObject后它不允许对象的赋值操作）
    friend QDataStream &operator<<(QDataStream &stream, const ExampleData &d)
    {
        stream << d.str1<<d.i1<<d.b1<<d.f1<<d.hex1;     //注意，该例子为导出变量1（实际为导出需要序列化输出保存的成员变量）
        return stream;
    }
    friend QDataStream &operator>>(QDataStream &stream, ExampleData &d)
    {
        stream >> d.str2>>d.i2>>d.b2>>d.f2>>d.hex2;     //注意，该例子为载入变量2（实际应与导出变量及其顺序保持一致）
        return stream;
    }
};

#include "serialization.h"
#include <QVector>
#include <QDebug>
//序列化输出、输入测试函数
void testFunction()
{
    QVector<ExampleData> dataList;  //or QList、QMap、QHash
    for(int i = 0;i < 12;i ++)
    {
        ExampleData d;
        d.str1 = QString("string:%1").arg(i);
        d.i1 = qrand()%100;
        d.b1 = qrand()%2;
        d.f1 = (qrand()%100) / 0.1;
        d.hex1 = "123456789abcdefg";
        dataList.append(d);
    }
    Serialization::to_file(dataList,QString("./testFunctionFile"));
    dataList.clear();
    Serialization::from_file(dataList,QString("./testFunctionFile"));
    dataList.clear();
}

#endif // EXAMPLEDATA_H
